package com.cuit.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author chishuiLi     Email:1192537447@qq.com
 * @Description
 */
@Configuration
public class ShiroConfig {

    //shiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        //添加shiro的内置过滤器
        /*
            anon:   无需认证就可以访问
            authc:  必须认证了才可以访问
            user:   必须拥有 记住我 功能才能那个用
            perms:  拥有对某个资源的权限才能访问
            root:   拥有某个角色权限才能访问
         */
        //拦截
        Map<String, String> filterMap = new LinkedHashMap<>();

        //授权,正常情况下，没有授权，会跳转到未授权页面
        //前面路径与controller中保持一致
        filterMap.put("/","authc");
        filterMap.put("/index","authc");
        filterMap.put("/admin/index","authc"); //登录了才能访问admin/index
        filterMap.put("/order-info/order/list","perms[order]"); //订单界面权限访问
        filterMap.put("/user/selectById","perms[select]");  //只有拥有select权限的才能访问
        filterMap.put("/user/updateUser","perms[select]");
        filterMap.put("/admin/admin","perms[admin]");
        filterMap.put("/comment/selectById","perms[comment]");

        //退出登录
        filterMap.put("/logout","logout");


//        filterMap.put("/demo/add","authc"); //这个路径要与@RequestMapping()中的路径一致
//        filterMap.put("/demo/update","authc");
//        filterMap.put("/demo/*","authc");

        bean.setFilterChainDefinitionMap(filterMap);

        //设置登录的请求
        bean.setLoginUrl("/toLogin");
        //未授权页面
        bean.setUnauthorizedUrl("/noauth");

        return bean;
    }

    //DefaultWebSecurityManager:2
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联UserRealm
        securityManager.setRealm(userRealm);

        //关联SessionManager
        securityManager.setSessionManager(SessionManager());

        return securityManager;
    }

    @Bean
    public DefaultWebSessionManager SessionManager(){

        //解决重定向后url带jsonId
        DefaultWebSessionManager defaultSessionManager = new DefaultWebSessionManager();
        //将sessionIdUrlRewritingEnabled属性设置成false
        defaultSessionManager.setSessionIdUrlRewritingEnabled(false);
        return defaultSessionManager;
    }



    //创建 realm 对象 自定义 :1
    @Bean
    public UserRealm userRealm(){
        return new UserRealm();
    }

    //整合ShiroDialect:用来整合shiro thymeleaf
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }



}
